<template>
  <div class="wrap">
    <div class="show-title">
      <span>状态消息</span>
    </div>
    <!-- 选择 -->
    <el-form
      :inline="true"
      :model="ruleform"
      ref="form"
      :rules="rules"
      class="demo-form-inline form-wrap"
    >
      <el-form-item label="设备别名" prop="alias">
        <el-input
          v-model="ruleform.alias"
          message
          placeholder="请输入"
          style="width:380px"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="start_time">
        <el-date-picker
          time-arrow-control
          v-model="ruleform.start_time"
          type="datetime"
          placeholder="开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="end_time">
        <el-date-picker
          time-arrow-control
          v-model="ruleform.end_time"
          type="datetime"
          placeholder="结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上上级分组">
        <el-select
          ref="prevSelect"
          @change="fnOrgPrev"
          @input="fnClickPrev"
          :multiple="true"
          :multiple-limit="1"
          v-model="ruleform.gfather_name"
          placeholder="请选择"
        >
          <el-option
            v-for="item in org_arr_prev"
            :key="item.gfather_name"
            :label="item.gfather_name"
            :value="item.gfather_name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上级分组">
        <el-select
          ref="nextSelect"
          @input="fnClickNext"
          :multiple="true"
          :multiple-limit="1"
          v-model="ruleform.father_name"
          placeholder="请选择"
        >
          <el-option
            v-for="item in org_arr_next"
            :key="item.father_name"
            :label="item.father_name"
            :value="item.father_name"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="onSubmit('form')"
          >查询</el-button
        >
        <el-button
          type="success"
          :loading="exportLoading"
          :disabled="exportDisable"
          plain
          @click="onExport2excel('from')"
          >导出日志</el-button
        >
        <el-button type="warning" plain @click="onReset('form')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table
      class="form-table"
      ref="table"
      :data="tableData"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
      ></el-table-column>
      <el-table-column
        prop="alias"
        label="设备别名"
        min-width="200"
        align="center"
      >
        <template slot-scope="scope">{{ decodeURI(scope.row.alias) }}</template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="设备状态"
        min-width="130"
        align="center"
      >
        <template slot-scope="scope">
          <span class="gray" v-if="scope.row.type == 1">离线</span>
          <span class="green" v-if="scope.row.type == 2">在线</span>
          <span class="red" v-if="scope.row.type == 3">已取消</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="gfather_name"
        label="上上级分组"
        min-width="180"
        align="center"
      >
        <template slot-scope="scope">{{
          decodeURI(scope.row.gfather_name)
        }}</template>
      </el-table-column>
      <el-table-column
        prop="father_name"
        label="上级分组"
        min-width="180"
        align="center"
      >
        <template slot-scope="scope">{{
          decodeURI(scope.row.father_name)
        }}</template>
      </el-table-column>
      <!-- <el-table-column
        prop="title"
        label="消息描述"
        min-width="300"
        align="center"
      >
        <template slot-scope="scope">{{ decodeURI(scope.row.title) }}</template>
      </el-table-column> -->
      <el-table-column
        prop="ts"
        label="创建时间"
        min-width="120"
        align="center"
         sortable="custom"
      >
        <template slot-scope="scope">{{
          getYMDHMS_transed(scope.row.ts)
        }}</template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="pageSizesArr"
      :page-size="pagesize"
      :total="total"
      :current-page="currpage"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!--批量导出日志  -->
    <!-- <div class="batch-btn-con">
      <el-button
        class="batchDelect"
        type="success"
        :loading="exportLoadingBatch"
        :disabled="exportDisableBatch"
        plain
        @click="batchExport2excel"
        v-permissions="{
          url: ['message/device/status/export2excel'],
          hasDisplay: true,
        }"
        >导出选中日志</el-button
      >
    </div> -->
  </div>
</template>

<script>
import axios from "axios";
import { getData, postData } from "@/request/http";
import { getYMDHMS } from "@/utils/Tool";
export default {
  name: "palylogs",
  props: {},
  data() {
    return {
      currpage: 1, //当前页
      pagesize: globalPageSizes[0], //初始显示，一页几个
      pageSizesArr: globalPageSizes, //globalPageSizes在public--config中全局配置，每页显示个数数组
      total: 0, // 总数
      tableData: [], // 表格数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      ruleform: {
        alias: "",
        start_time: "",
        end_time: "",
        father_name: "",
        gfather_name: "",
      },
      rules: {
        alias: [
          { required: false, message: "请输入设备别名", trigger: "blur" },
        ],
        start_time: [
          { required: false, message: "请输入开始时间", trigger: "blur" },
        ],
        end_time: [
          { required: false, message: "请输入结束时间", trigger: "blur" },
        ],
      },
      org_arr_prev: [],
      org_arr_next: [],
      exportDisable: false,
      exportLoading: false,
      exportDisableBatch: false,
      exportLoadingBatch: false,
      time_order: "DESC",  //默认倒序
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.search();
    this.get_gfather_name();
  },
  methods: {
    fnClickPrev() {
      !!this.$refs.prevSelect && this.$refs.prevSelect.handleClose();
    },
    fnClickNext() {
      !!this.$refs.nextSelect && this.$refs.nextSelect.handleClose();
    },
    /*
     *@Des: 上上级分钟名称
     *@Author: hhh, 2021-12-02 17:50:44
     */
    get_gfather_name() {
      postData("/adminweb/org/list/tree")
        .then((res) => {
          this.org_arr_prev =
            !res || !res.datas || !res.datas.gfathers ? [] : res.datas.gfathers;
        })
        .catch((res) => {
          this.$message.error("error");
        });
    },

    /*
     *@Des: 上上级chnage
     *@Author: hhh, 2021-12-03 10:16:07
     */
    fnOrgPrev(val) {
      this.getOrgNextData(val);
      this.ruleform.father_name = "";
      if (val == "") {
        this.org_arr_next = [];
      }
    },

    /*
     *@Des: 获取下级数据
     *@Author: hhh, 2021-12-03 10:16:52
     */
    getOrgNextData(val) {
      console.log(val);
      let arr = this.org_arr_prev;
      console.log(arr);
      if (Array.isArray(arr) && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          let gfather_name = arr[i]["gfather_name"];
          if (gfather_name == val) {
            this.org_arr_next = !arr[i]["fathers"] ? [] : arr[i]["fathers"];
          }
        }
      }
    },
    //转换
    getYMDHMS_transed(ts) {
      return getYMDHMS(ts);
    },
    //查询
    onSubmit(formName) {
      this.currpage = 1;
      this.search();
    },
    //初始列表
    search() {
      let alias_sub = this.ruleform.alias;
      let start_time_sub = this.ruleform.start_time;
      let end_time_sub = this.ruleform.end_time;
      let dataParams = {
        alias: alias_sub ? alias_sub : "",
        start_time: start_time_sub ? start_time_sub.getTime() : "",
        end_time: end_time_sub ? end_time_sub.getTime() : "",
        gfather_name: this.ruleform.gfather_name[0],
        father_name: this.ruleform.father_name[0],
        page: this.currpage,
        page_num: this.pagesize,
         time_order: this.time_order,
      };
      postData("/adminweb/message/device/status/list", dataParams)
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            if (!res.datas || !res.datas.messages) {
              return;
            }
            let messages = res.datas.messages;
            this.tableData = messages;
            this.total = res.datas.total_count;
            //  console.log(this.tableData);
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //导出查询日志
    onExport2excel() {
      this.exportDisable = true;
      this.exportLoading = true;
      let alias_sub = this.ruleform.alias;
      let start_time_sub = this.ruleform.start_time;
      let end_time_sub = this.ruleform.end_time;
      let dataParams = {
        alias: alias_sub ? alias_sub : "",
        start_time: start_time_sub ? start_time_sub.getTime() : "",
        end_time: end_time_sub ? end_time_sub.getTime() : "",
        gfather_name: this.ruleform.gfather_name[0],
        father_name: this.ruleform.father_name[0],
        messages: this.batchDelectId.length == 0 ? [] : this.batchDelectId,
        page: this.currpage,
        page_num: this.pagesize,
         time_order: this.time_order,
      };
      postData("/adminweb/message/device/status/export2excel", dataParams, {
        timeout: 120000,
      })
        .then((res) => {
          // console.log(res);ai
          this.exportDisable = false;
          this.exportLoading = false;
          if (res.code == 0) {
            let excel_uri = res.datas.excel_uri;
            let download_url = baseUrl + "/" + excel_uri;
            window.location.href = download_url;
          } else if (res.code == -59026) {
            this.$message.error("未找到设备的状态消息记录");
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.exportDisable = false;
          this.exportLoading = false;
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //批量导出日志
    // batchExport2excel(index, row) {
    //   this.exportDisableBatch = true;
    //   this.exportLoadingBatch = true;
    //   if (this.batchDelectId.length == 0) {
    //     this.$message.error("请先勾选要批量操作的行");
    //     return;
    //   }
    //   var that = this;
    //   postData(
    //     "/adminweb/message/device/status/export2excel",
    //     {
    //       messages: that.batchDelectId,
    //     },
    //     {
    //       timeout: 120000,
    //     }
    //   )
    //     .then((res) => {
    //       console.log(res);
    //       that.exportDisableBatch = false;
    //       that.exportLoadingBatch = false;
    //       if (res.code == 0) {
    //         let excel_uri = res.datas.excel_uri;
    //         let download_url = baseUrl + "/" + excel_uri;
    //         window.location.href = download_url;
    //       } else if (res.code == -58021) {
    //         that.$message.error("未找到设备的播放日志");
    //       } else if (res.code == -56055) {
    //         return;
    //       } else {
    //         that.$message.error(res.code + "，" + res.msg);
    //       }
    //     })
    //     .catch((res) => {
    //       that.exportDisableBatch = false;
    //       that.exportLoadingBatch = false;
    //       this.$message.error(res.code + "，" + res.msg);
    //     });
    // },
    onReset() {
      this.currpage = 1;
      this.ruleform.alias = "";
      this.ruleform.start_time = "";
      this.ruleform.end_time = "";
      this.ruleform.gfather_name = "";
      this.ruleform.father_name = "";
      this.search();
       //清空table的checked
      this.$refs.table.clearSelection();
    },

    // 获取row的key值
    getRowKeys(row) {
      return row.message_id;
    },
    //点击,第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
      this.search();
    },
    //每页有几条
    handleSizeChange(psize) {
      this.currpage = 1;
      this.pagesize = psize;
      this.search();
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      // console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["message_id"]);
        }
      }
    },
   //排序
    changeSort(data) {
      if (data.order == "descending") {
        this.time_order = "DESC";
      } else if (data.order == "ascending") {
        this.time_order = "ASC";
      }
      this.currpage = 1;
      this.search();
      // console.log(val);
    },
  },
};
</script>
<style scoped>
.form-wrap {
  padding-left: 20px;
}
.wrap >>> .el-select-dropdown__item.selected {
  color: #ccc !important;
}
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  background-color: #fff;
  padding: 15px;
  overflow: hidden;
}
.form-table {
  margin-top: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  background: #fff;
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}

/*title*/
.show-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  background: #c0dee2;
  padding-left: 15px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  border-radius: 12px 12px 0 0;
  font-weight: bold;
}

.show-title::before {
  content: "";
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 27px;
}

.show-title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
/* title显示名称id */
.wrap >>> .table-wrap {
  border: none;
  box-shadow: none;
}
.wrap >>> .el-divider__text.is-center {
  font-size: 17px;
}
.wrap >>> .el-divider__text {
  background: #c0dee2;
  padding: 15px;
  border-radius: 8px;
}
.title-list {
  background: #fff;
  padding-left: 20px;
  height: 80px;
}
.title-list .txt {
  height: 10px;
}
.gray {
  color: gray;
}
.green {
  color: #2cc52c;
}
.red {
  color: #cf3535;
}
</style>
